package com.holy.sh.module_base_mvp.utils;

import android.support.annotation.StringRes;
import android.text.TextUtils;


import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by victor on 2018/3/31.
 * Email: victorhhl@163.com
 * Describtion :
 */

public class StringUtils {
    /**
     * double转String,保留小数点后两位
     * @param num
     * @return
     */
    public static String doubleToString2(double num){
        //使用0.00不足位补0，#.##仅保留有效位
        return new DecimalFormat("0.00").format(num);
    }

    public static String doubleToString4(double num){
        //使用0.00不足位补0，#.##仅保留有效位
        return new DecimalFormat("0.0000").format(num);
    }
    public static String appenString(List<String> ss){
        String s=null;
        StringBuilder builder=new StringBuilder();
      if (ss!=null){
          for (int i = 0; i <ss.size() ; i++) {
              builder.append(ss.get(i)+",");
          }
          s=builder.toString().substring(0,builder.toString().length()-1);
      }
        return s;
    }


        public static String getKm(String km){
            String kkm="";
        if (km!=null&&!km.equals("")){
            Double kml= Double.parseDouble(km);
            if (kml>1000){
                kkm=Math.floor((kml/1000)*10)/10 +" km";
            }else {
                kkm=km +"m";
            }
        }
            return kkm;
        }

    public static String getKmOutKm(String km){
        String kkm="";
        if (km!=null&&!km.equals("")){
            Double kml= Double.parseDouble(km);
            if (kml>1000){
                kkm=Math.floor((kml/1000)*10)/10+"";
            }else {
                kkm=km +"";
            }
        }
        return kkm;
    }


    public static boolean isKm(String km){
        String kkm="";
        if (km!=null&&!km.equals("")){
            Double kml= Double.parseDouble(km);
            if (kml>1000){
              return true;
            }else {
             return false;
        }
        }
       return false;
    }


    public static String setTextWithColor(String text, String color){
            //"今天<font color='#FF0000'>天气不错</font>";
            return "<font color="+color+">"+text+"</font>";

        }

    public static String setTextWithColorAndsize(String text, String color, String size){
        //"今天<font color='#FF0000'>天气不错</font>";

        return "<font color="+color+"><"+size+">"+text+"</small></font>";


    }

    /**
     * 手机号验证
     *
     * @param str
     * @return 验证通过返回true
     */
    public static boolean isMobile(String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^[1][3,4,5,6,7,8,9][0-9]{9}$");
        m = p.matcher(str);
        b = m.matches();
        return b;
    }

    //添加换行
    public static String AddTextSpace(String str){
        if(!TextUtils.isEmpty(str)){
            StringBuffer buffer = new StringBuffer();
            for(int i=1;i<=str.length();i++){
                buffer.append(str.trim().substring(i-1,i));
                if(i % 4 == 0 && i != str.length()){
                    buffer.append("   ");
                }
            }
            return buffer.toString();
        }
        return "";
    }

    public static List<String> getStrings(String text, String fgf){
        List<String> ss;
        if (!TextUtils.isEmpty(text)) {
            String[] split = text.split(fgf);
            ss=new ArrayList<>();
            for (int i=0;i<split.length;i++){
                ss.add(split[i]);
            }
            return ss;
        }else {
            return null;
        }
    }

    public static String appendListString(List<String> data){
        if (!ListTools.empty(data)){
            StringBuilder builder=new StringBuilder();
            for (int i = 0; i <data.size() ; i++) {
                if (i==data.size()-1){
                    builder.append(data.get(i));
                }else {
                    builder.append(data.get(i)+",");
                }

            }
            return builder.toString();
        }
        return null;
    }
    public static String appendListInteger(List<Integer> data){
        if (!ListTools.empty(data)){
            StringBuilder builder=new StringBuilder();
            for (int i = 0; i <data.size() ; i++) {
                if (i==data.size()-1){
                    builder.append(data.get(i)+"");
                }else {
                    builder.append(data.get(i)+",");
                }

            }
            return builder.toString();
        }
        return null;
    }

    //
    public static String replace(String ss, String old, String newS){

     return ss.replace(old,newS);
    }

    public static boolean noEmpty(CharSequence source) {
        return !TextUtils.isEmpty(source);
    }

    public static boolean noEmpty(CharSequence... source) {
        for (CharSequence charSequence : source) {
            if (TextUtils.isEmpty(charSequence)) return false;
        }
        return true;
    }


    public static boolean isEmptyWithNull(String source) {
        return source == null || TextUtils.isEmpty(source.trim()) || TextUtils.equals(source, "null") || TextUtils.equals(source, "Null") || TextUtils.equals(source, "NULL");
    }

    public static String trim(String source) {
        if (noEmpty(source)) {
            return source.trim();
        }
        return source;

    }

    public static boolean contains(String source, String key) {
        if (noEmpty(source)) {
            return source.contains(key);
        }
        return false;
    }

    public static CharSequence trim(CharSequence cs) {
        int len = cs.length();
        int st = 0;

        while ((st < len) && (cs.charAt(st) <= ' ')) {
            st++;
        }
        while ((st < len) && (cs.charAt(len - 1) <= ' ')) {
            len--;
        }
        return ((st > 0) || (len < cs.length())) ? cs.subSequence(st, len) : cs;
    }


//    /**
//     * 从资源文件拿到文字
//     */
//    public static String getString(@StringRes int strId, Object... objs) {
//        if (strId == 0) return null;
//      //  return AppContext.getAppContext().getResources().getString(strId, objs);
//    }


}
